package com.itheima.web.job;

import com.itheima.service.db.AnalysisByDayService;
import com.itheima.service.db.UserService;
import com.itheima.service.mongo.SoundService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyJob {

    @Autowired
    private JobManager jobManager;

    @Reference
    AnalysisByDayService analysisByDayService;
    @Reference
    private SoundService soundService;
    @Reference
    private UserService userService;


    @Scheduled(cron = "0 0 0/1 * * ?")//每小时
    //@Scheduled(cron = "0 0/1 * * * ?")//每分钟
    public void saveOrUpdateAnalysis(){
        analysisByDayService.saveOrUpdate();
        System.err.println("今日统计数据已保存或更新....");
    }

    //@Scheduled(cron = "0 0 4 * * ?")//todo 每天凌晨4点
    @Scheduled(cron = "0/2 * * * * ?")//每5分钟
    public void resetRemainingTimes(){
        List<Long> userIds = userService.getUserIds();
        soundService.resetRemainingTimes(userIds);

        System.err.println("重置今天传音次数");
    }

    @Scheduled(cron = "0 0 4 * 1/6 ?")//半年一次
    public void soundGC(){
        jobManager.soundGC();
        System.err.println("重置今天传音次数");
    }
}
